import queue
import threading
import time

import requests


class Download(threading.Thread):
    def __init__(self, manager):
        threading.Thread.__init__(self)
        self.manager = manager
        pass

    def save(self):
        pass

    def run(self):
        while True:
            try:
                if not self.manager.url_empty():
                    url_type, layer, url = self.manager.get_url()
                    print("get url {}".format(url))
                    re = requests.get(url)
                    # print(re.encoding)
                    re.encoding = "utf-8"
                    print("add parser")
                    print((url_type, layer, re.text))
                    self.manager.add_parser((url_type, layer, re.text))
                    # print(re.text)
                    # print(re.content)
                else:
                    # print("download sleep")
                    time.sleep(1)
            except Exception as e:
                print(e)

    def get_file_header(self):
        return {
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"
        }